package com.ruoyi.personnel.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.personnel.domain.Employee;
import com.ruoyi.personnel.service.LikeService;
import com.ruoyi.personnel.utils.Constants;
import com.ruoyi.personnel.utils.DynamicInfo;
import com.ruoyi.personnel.utils.PhoneInfo;
import com.ruoyi.personnel.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author : 张培浩
 * @date : 2023/3/8 17:22
 **/
@Service
public class LikeServiceImpl implements LikeService {
    // 引入Redis
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private EmployeeServiceImpl employeeService;

    /**
     * 获取对应员工的点赞排名信息
     * @param empId
     * @return
     */
    @Override
    public int getRanking(Integer empId) {
        ZSetOperations zSet = redisTemplate.opsForZSet();
        Long aLong = zSet.reverseRank(Constants.SALES_LIST,empId);
        return aLong == null ? -1 : aLong.intValue();
    }

    /**
     * 实现动态点赞数据的自增
     * @param empId
     */
    @Override
    public void isLike(int empId) {
        ZSetOperations zSet = redisTemplate.opsForZSet();
        zSet.incrementScore(Constants.SALES_LIST,empId,1);
        // 添加点赞动态时间
        long currentTimeMillis = System.currentTimeMillis();
        // 获取到员工名称
        Employee employee = employeeService.selectEmployeeByEmpId(empId);
        String msg = currentTimeMillis + Constants.separator + employee.getEmpName();
        System.out.println("----->msg:"+msg);
        // 将员工点赞动态时间添加到redis的list中
        ListOperations opsForList = redisTemplate.opsForList();
        opsForList.leftPush(Constants.BUY_DYNAMIC,msg);
    }

    /**
     * 实现排行展示信息
     * @return
     */
    @Override
    public List<PhoneInfo> getLikeList(){
        // 按照点赞量排行，取出前7名
        ZSetOperations zSet = redisTemplate.opsForZSet();
        Set<ZSetOperations.TypedTuple> set = zSet.reverseRangeWithScores(Constants.SALES_LIST, 0, 6);
        List<PhoneInfo> list = new ArrayList<PhoneInfo>();
        for (ZSetOperations.TypedTuple typedTuple : set) {
            PhoneInfo phoneInfo = new PhoneInfo();
            Object value =  typedTuple.getValue();
            Double parseDouble = Double.parseDouble(value.toString());
            int empId = parseDouble.intValue();
            phoneInfo.setName(employeeService.selectEmployeeByEmpId(empId).getEmpName());
            phoneInfo.setSales(typedTuple.getScore().intValue());
            list.add(phoneInfo);
        }
        return list;
    }

    /**
     * 获取当前最近的三次点赞
     * @return
     */
    @Override
    public List<DynamicInfo> getDataLike() {
        ListOperations opsForList = redisTemplate.opsForList();
        List<DynamicInfo> dynamicList = new ArrayList<DynamicInfo>();
        for (int i = 0; i < 10; i++) {
            String index = (String) opsForList.index(Constants.BUY_DYNAMIC, i);
            if (StringUtils.isEmpty(index)) {
                break;
            }
            String[] split = index.split(Constants.separator);
            long time = Long.valueOf(split[0]);
            String name = split[1];
            DynamicInfo vo = new DynamicInfo();
            vo.setName(name);
            vo.setTime(StringUtil.showTime(new Date(time)));
            dynamicList.add(vo);
        }
        return dynamicList;
    }

}
